package com.erp.servlet;

import com.erp.bean.Role;
import com.erp.service.RoleService;
import com.erp.service.impl.RoleServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author zch
 * 修改部门名称控制器
 */
@WebServlet("/roleedit.action")
public class RoleEditServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        RoleService roleService = new RoleServiceImpl();
        String ridstr = req.getParameter("rid");
        System.out.println(ridstr);
        int rid = Integer.valueOf(ridstr);
        String type = req.getParameter("type");
        if(type.equals("1")){
            Role role = roleService.findByRid(rid);
            req.setAttribute("role",role);
            req.getRequestDispatcher("roleedit.jsp").forward(req,resp);
        }else if(type.equals("2")){
            String rname = req.getParameter("rname");
            Role role = new Role(rid,rname);
            boolean flag = roleService.update(role);
            System.out.println(flag);
            resp.sendRedirect("rolelist.action");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
